<?php
class Ranking extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
        if (file_exists('application/controllers/install.php'))
            redirect('install');
        $this->load->model('works_model');
        $this->load->model('college_model');
        $this->load->model('category_model');
        $this->load->model('poll_model');
        $this->load->library('session');
    }

    function index() {
        $data['title'] = '排行榜！';
        $data['rank'] = $this->poll_model->rank();
        $data['category'] = $this->category_model->getAll();
        $data['check'] = $this->category_model->getAll();

        $this->load->view('templates/header', $data);
        $this->load->view('ranking', $data);
        $this->load->view('templates/footer', $data);
    }

    function search() {
        $data['title'] = '排行榜';
        $id = $this->input->get('id');
        $data['check'][] = $this->category_model->select($id);

        if (empty($data['check'])){  // if $id not existed, redirect
            $data['title'] = '出错啦';
            $data['words'] = '查询id错误';
            $this->load->view('templates/header', $data);
            $this->load->view('poster_admin/404', $data);
            $this->load->view('templates/footer', $data);
        } else {
            $data['rank'] = $this->poll_model->rank();
            $data['category'] = $this->category_model->getAll();
            $this->load->view('templates/header', $data);
            $this->load->view('ranking', $data);
            $this->load->view('templates/footer', $data);
        }
    }
}